package com.bdqn.tour.servlet.Tj;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/submitOrder")
public class OrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        try {
            // 获取表单数据
            String ticketId = request.getParameter("ticketId");
            // 添加null检查
            String departureCity = request.getParameter("departureCity");
            departureCity = (departureCity != null) ? URLDecoder.decode(departureCity, "UTF-8") : "";
            
            String arrivalCity = request.getParameter("arrivalCity");
            arrivalCity = (arrivalCity != null) ? URLDecoder.decode(arrivalCity, "UTF-8") : "";
            
            String departureDate = request.getParameter("departureDate");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String price = request.getParameter("price");

            // 验证必要参数
            if (ticketId == null || departureCity.isEmpty() || arrivalCity.isEmpty() 
                || departureDate == null || startTime == null || endTime == null || price == null) {
                response.getWriter().write("<script>alert('请求参数不完整！');history.back();</script>");
                return;
            }

            Connection conn = null;
            PreparedStatement pstmt = null;

            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/tourproject?useUnicode=true&characterEncoding=utf8";
                conn = DriverManager.getConnection(url, "root", "123456");

                String sql = "INSERT INTO orders (id, departure_city, arrival_city, " +
                            "departure_date, start_time, end_time, price) " +
                            "VALUES (?, ?, ?, ?, ?, ?, ?)";

                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, ticketId);
                pstmt.setString(2, departureCity);
                pstmt.setString(3, arrivalCity);
                pstmt.setString(4, departureDate);
                pstmt.setString(5, startTime);
                pstmt.setString(6, endTime);
                pstmt.setDouble(7, Double.parseDouble(price));

                int result = pstmt.executeUpdate();

                if (result > 0) {
                    response.getWriter().write("<script>alert('订单创建成功！');window.location.href='index.jsp';</script>");
                } else {
                    response.getWriter().write("<script>alert('订单创建失败！');history.back();</script>");
                }

            } catch (Exception e) {
                e.printStackTrace();
                response.getWriter().write("<script>alert('系统错误：" + e.getMessage() + "');history.back();</script>");
            } finally {
                try {
                    if (pstmt != null) pstmt.close();
                    if (conn != null) conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("<script>alert('系统错误：" + e.getMessage() + "');history.back();</script>");
        }
    }
} 